//
//  TTComposeCollection.swift
//  TTWeibo
//
//  Created by 陶宏路 on 16/7/26.
//  Copyright © 2016年 陶路路. All rights reserved.
//

import UIKit

let identifier:String = "identifier"

class TTComposeCollection: UICollectionView ,UICollectionViewDataSource{

    
    var imageArray:[UIImage] = [UIImage]()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        backgroundColor = UIColor.whiteColor()
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI(){
        
        let flowLayout = self.collectionViewLayout as? UICollectionViewFlowLayout
        flowLayout?.minimumInteritemSpacing = 5
        flowLayout?.minimumLineSpacing = 5
        let margin:CGFloat = 5
        let width :CGFloat = ((SCREENW - 20) - margin * 2) / 3
        dataSource = self
        flowLayout?.itemSize = CGSizeMake(width, width)
        registerClass(composeCollectionCell.self, forCellWithReuseIdentifier: identifier)
        self.hidden = true
    }
}

extension TTComposeCollection{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        if imageArray.count == 0 || imageArray.count == 9{
            
            return imageArray.count
            
        }else{
            
            self.hidden = false
            
            return imageArray.count + 1
            
        }
        
    }
    
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath) as! composeCollectionCell
        
        if indexPath.item == imageArray.count{
            
//            cell.buttomImage = nil
            cell.setImageAndTag(nil, tag: 10086)
            
        }else{
            
            cell.setImageAndTag(imageArray[indexPath.item], tag: indexPath.item)
            
        }
        
        cell.block = { [weak self] in
        
            self!.imageArray.removeAtIndex(indexPath.item)
            
            self!.reloadData()
            
            if self!.imageArray.count <= 0{
                
                self!.hidden = true
                
            }else{
                
                self!.hidden = false
            }
            
        }
        
        return cell
    }
}

class composeCollectionCell:UICollectionViewCell {
    
    var block:(()->())?
    
    func setImageAndTag(buttomImage:UIImage?,tag:Int?){
        
        pictureButton.setImage(buttomImage, forState: UIControlState.Normal)
        
        
        pictureButton.tag = tag ?? 0
        
        pictureButton.subviews.last?.hidden = false
        
                    if buttomImage == nil{
        
                        cancelButton.hidden = true
                    }

    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setUpUI(){
        
        addSubview(pictureButton)

        pictureButton.addSubview(cancelButton)

        pictureButton.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        cancelButton.snp_makeConstraints { (make) -> Void in
            
            make.top.trailing.equalTo(pictureButton)
        }
    }
    
    lazy var pictureButton:UIButton = {
        let picture = UIButton()
        picture.setBackgroundImage(UIImage(named: "compose_pic_add"), forState: UIControlState.Normal)
        picture.setBackgroundImage(UIImage(named: "compose_pic_add_highlighted"), forState: UIControlState.Highlighted)
        picture.addTarget(self, action: "didPictureButton:", forControlEvents: UIControlEvents.TouchUpInside)
        return picture
    }()
  
    lazy var cancelButton:UIButton = {
        
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        button.hidden = true
        button.addTarget(self, action: "didCancelPictureButton:", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    
    
    func didCancelPictureButton(cancelButton:UIButton){
     
        cancelButton.hidden = true
        
        block?()
        
}


    func didPictureButton(sender:UIButton){
        
        NSNotificationCenter.defaultCenter().postNotificationName("didPictureButton", object:sender)
    }
}